package org.jetnuke.dao.sqlserver;

import org.apache.log4j.Logger;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;

import net.jforum.JForumExecutionContext;
import net.jforum.util.preferences.SystemGlobals;

import org.jetnuke.entities.Stories;

public class SqlServerStoriesDAO extends
		org.jetnuke.dao.generic.GenericStoriesDAO {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(SqlServerStoriesDAO.class);

	public Stories getTopByTopic(int topic) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getTopByTopic(int) - start"); //$NON-NLS-1$
		}

		String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
		String q2 = SystemGlobals.getSql("StoryModel.getTopByTopic");

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(q1 + " 1 " + q2);
		p.setInt(1, topic);
		ResultSet rs = p.executeQuery();

		Stories s = new Stories();

		if (rs.next()) {
			fillStoryFromResultSet(s, rs);
		}

		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getTopByTopic(int) - end"); //$NON-NLS-1$
		}
		return s;
	}

	public List selectOrderByCounterDescLimit(int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectOrderByCounterDescLimit(int) - start"); //$NON-NLS-1$
		}

		String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
		String q2 = SystemGlobals
				.getSql("StoryModel.selectOrderByCounterDescLimit");

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(q1 + " " + limit + " " + q2);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectOrderByCounterDescLimit(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectVotedOrderScoreDesc(int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectVotedOrderScoreDesc(int) - start"); //$NON-NLS-1$
		}

		String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
		String q2 = SystemGlobals
				.getSql("StoryModel.selectVotedOrderScoreDesc");

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(q1 + " " + limit + " " + q2);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectVotedOrderScoreDesc(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectCommentedOrderCommentDesc(int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectCommentedOrderCommentDesc(int) - start"); //$NON-NLS-1$
		}

		String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
		String q2 = SystemGlobals
				.getSql("StoryModel.selectCommentedOrderCommentDesc");

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(q1 + " " + limit + " " + q2);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectCommentedOrderCommentDesc(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List selectOrderBySidDescLimit(int limit) throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("selectOrderBySidDescLimit(int) - start"); //$NON-NLS-1$
		}

		String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
		String q2 = SystemGlobals
				.getSql("StoryModel.selectOrderBySidDescLimit");

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(q1 + " " + limit + " " + q2);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("selectOrderBySidDescLimit(int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List getStoriesByTopid_SidDesc(int topic, int limit)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getStoriesByTopid_SidDesc(int, int) - start"); //$NON-NLS-1$
		}

		PreparedStatement p;

		if (topic == 0) {
			String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
			String q2 = SystemGlobals.getSql("StoryModel.getStoriesSidDesc");
			p = JForumExecutionContext.getConnection().prepareStatement(
					q1 + " " + limit + " " + q2);
		} else {
			String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
			String q2 = SystemGlobals.getSql("StoryModel.getStoriesSidDesc1");
			p = JForumExecutionContext.getConnection().prepareStatement(
					q1 + " " + limit + " " + q2);
			p.setInt(1, topic);
		}

		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getStoriesByTopid_SidDesc(int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}

	public List getStoriesByCategory_SidDesc(int catid, int limit)
			throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("getStoriesByCategory_SidDesc(int, int) - start"); //$NON-NLS-1$
		}

		String q1 = SystemGlobals.getSql("GenericModel.selectByLimit");
		String q2 = SystemGlobals
				.getSql("StoryModel.getStoriesByCategory_SidDesc");

		PreparedStatement p = JForumExecutionContext.getConnection()
				.prepareStatement(q1 + " " + limit + " " + q2);
		p.setInt(1, catid);
		ResultSet rs = p.executeQuery();
		List list = this.processSelectAll(rs);
		rs.close();
		p.close();

		if (logger.isDebugEnabled()) {
			logger.debug("getStoriesByCategory_SidDesc(int, int) - end"); //$NON-NLS-1$
		}
		return list;
	}
}
